import { ref } from 'vue';

export default function useValidate() {
  const registerForm = ref({
    username: '',
    password: '',
    confirmPassword: ''
  });

  const validateUsername = (inputVal: unknown): boolean | string => {
    if (typeof inputVal !== 'string' && inputVal == null) {
      return '用户名不能为空';
    }

    if (!inputVal) {
      return '用户名不能为空';
    }

    if (typeof inputVal === 'string') {
      if (inputVal.length < 3 || inputVal.length > 12) {
        return '用户名长度必须在3-12位之间';
      }
    }

    return true;
  };
  const validatePassword = (inputVal: unknown): boolean | string => {
    if (typeof inputVal !== 'string' && inputVal == null) {
      return '密码不能为空';
    }

    if (!inputVal) {
      return '密码不能为空';
    }

    if (typeof inputVal === 'string') {
      if (inputVal.length < 6 || inputVal.length > 12) {
        return '密码长度必须在6-12位之间';
      }
    }

    return true;
  };
  const validateConfirmPassword = (
    inputVal: unknown,
    [password]: unknown[]
  ): boolean | string => {
    if (inputVal !== password) {
      return '两次密码输入必须一致';
    }

    return true;
  };
  const onRegisterHandler = () => {
    console.log('onRegisterHandler');
  };

  return {
    registerForm,
    validateUsername,
    validatePassword,
    validateConfirmPassword,
    onRegisterHandler
  };
}
